package ch.electronet.wjw.web.actions;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSON;
import net.sf.json.JSONSerializer;
import net.sf.json.JsonConfig;

import org.apache.struts2.interceptor.ServletResponseAware;

import com.opensymphony.xwork2.ActionSupport;

public abstract class JsonBaseAction extends ActionSupport implements ServletResponseAware {

	private static final long serialVersionUID = 1L;
	private HttpServletResponse response;

	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
	}

	protected abstract Object doJson(JsonConfig config);

	public String execute() throws Exception {
		JsonConfig config = new JsonConfig();
		Object json = doJson(config);
		JSON jsonObject = (JSON) JSONSerializer.toJSON(json, config);
		response.setContentType("application/json");
		response.setCharacterEncoding("ISO-8859-1");
		jsonObject.write(response.getWriter());
		return null;
	}

}
